﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using QLDV.ViewModels;

namespace QLDV.Models
{
    public class QLDVRepository : IQLDVRepository
    {
        private QLDVEntities db = new QLDVEntities();
        public QLDVEntities DB
        {
            get { return db; }
            set { db = value; }
        }

        public void CommitChanges()
        {
            DB.SaveChanges();
        }


        #region DoanVien
        public DOANVIEN LayDoanVien(int id)
        {
            return DB.DOANVIENs.FirstOrDefault(u => u.Id_Dv == id);
        }

        public void CapNhatDV(int madv, DOANVIEN dv)
        {
            DOANVIEN temp = db.DOANVIENs.Single(x => x.Id_Dv == madv);
            temp.HoTenDangDung = dv.HoTenDangDung;
            temp.HoTenKhaiSinh = dv.HoTenKhaiSinh;
            temp.NgaySinh = dv.NgaySinh;
            temp.QueQuan = dv.QueQuan;
            temp.NoiOHienNay = dv.NoiOHienNay;
            temp.DanToc = dv.DanToc;
            temp.TonGiao = dv.TonGiao;
            temp.NgheNghiep = dv.NgheNghiep;
            temp.TDVanHoa = dv.TDVanHoa;
            temp.TDChuyenMon = dv.TDChuyenMon;
            temp.TDLyLuanCT = dv.TDLyLuanCT;
            temp.NgoaiNgu = dv.NgoaiNgu;
            temp.TinhHinhSucKhoe = dv.TinhHinhSucKhoe;
            temp.KhenThuong = dv.KhenThuong;
            temp.KyLuat = dv.KyLuat;
            temp.NangKhieu = dv.NangKhieu;
            temp.SoTruong = dv.SoTruong;
            temp.SDT = dv.SDT;
            temp.Email = temp.Email;

            db.SaveChanges();

        }

        public void CapNhatGiaDinh(int id, GIADINH gd)
        {
            GIADINH temp = db.GIADINHs.Single(x => x.Id_Dv_Gd == id);
            temp.HoTen = gd.HoTen;
            temp.NamSinh = gd.NamSinh;
            temp.NgheNghiep = gd.NgheNghiep;
            temp.NoiO = gd.NoiO;
            temp.QuanHe = gd.QuanHe;
            temp.SoDT = gd.SoDT;

            db.SaveChanges();
        }

        public void XoaGiaDinh(int idDvGd)
        {
            db.GIADINHs.DeleteObject(db.GIADINHs.FirstOrDefault(u=>u.Id_Dv_Gd==idDvGd));
            db.SaveChanges();
        }

        public void ThemGiaDinh(GIADINH gd)
        {
            db.AddToGIADINHs(gd);
            try
            {
                db.SaveChanges();
            }catch(System.Exception ex)
            {
                int a = 1;
            }
        }

        #endregion


        #region Account
        public TAIKHOAN ValidateUser(string loginName, string password)
        {
            return DB.TAIKHOANs.FirstOrDefault(u => u.TenDangNhap == loginName && u.MatKhau == password && u.TrangThai == true);
        }
        public TAIKHOAN FindAccountID(string name)
        {
            return DB.TAIKHOANs.FirstOrDefault(u => u.TenDangNhap == name);
        }
        #endregion

        #region LoaiCSD
        public string InsertLoaiCSD(LoaiCSDModel loaiCSD)
        {
            try
            {
                if(loaiCSD.Id_LoaiCSDQL !=0)
                {
                    DB.LOAICSDs.AddObject(new LOAICSD
                    {
                        Ten = loaiCSD.Name,
                        MoTa = loaiCSD.Desc,
                        CapBac = (byte?)loaiCSD.Order,
                        DoanPhi = loaiCSD.DoanPhi,
                        Id_LoaiCSDQL = loaiCSD.Id_LoaiCSDQL,
                        CoDoanVien = loaiCSD.DoanVien,
                        CoChinhThuc = loaiCSD.CoChinhThuc,
                        NgayTao = DateTime.Now,
                        NgayChinhSuaCuoiCung = DateTime.Now

                    });
                }
                else
                {
                    DB.LOAICSDs.AddObject(new LOAICSD
                    {
                        Ten = loaiCSD.Name,
                        MoTa = loaiCSD.Desc,
                        CapBac = (byte?)loaiCSD.Order,
                        DoanPhi = loaiCSD.DoanPhi,
                        Id_LoaiCSDQL = null,
                        CoDoanVien = loaiCSD.DoanVien,
                        CoChinhThuc = loaiCSD.CoChinhThuc,
                        NgayTao = DateTime.Now,
                        NgayChinhSuaCuoiCung = DateTime.Now

                    });
                }
                CommitChanges();
                return "true";
            }
            catch (System.Exception ex)
            {
                return ex.Message;
            }
            
        }

        public bool UpdateLoaiCSD(LoaiCSDModel loaiCSD)
        {
            try
            {
                var obj = DB.LOAICSDs.Single(m => m.Id_LoaiCSD == loaiCSD.Id);
                obj.Ten = loaiCSD.Name;
                obj.MoTa = loaiCSD.Desc;
                obj.DoanPhi = loaiCSD.DoanPhi;
                obj.NgayChinhSuaCuoiCung = DateTime.Now;
                CommitChanges();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
            
        }

        public List<LOAICSD> GetAllLoaiCSD()
        {
            //var result = DB.GetAll_LoaiCSD();
            //return result.ToList();
            return DB.LOAICSDs.OrderBy(x => x.CapBac).OrderBy(x => x.Id_LoaiCSDQL).ToList();
        }

        public LOAICSD GetByIdLoaiCSD(int id)
        {
            var result = db.LOAICSDs.Single(x => x.Id_LoaiCSD == id);
            return result;
        }

        public bool DeleteLoaiCSD(int id)
        {
            var result = db.Delete_LoaiCSD(id);
            byte? check = result.First();
            if (check == 0) return true;
            else return false;
        }
        #endregion

        #region CSD
        public List<CSD> GetAllCsd()
        {
            var result = from p in DB.CSDs
                         where (p.TrangThai == true)
                         orderby p.Id_CSDQL
                         select p;
            return result.ToList();
        }

        public List<CSD> GetByIdLoaiCSD_CSD(int id_loaiCsd)
        {
            var result = from p in db.CSDs
                         where (p.Id_LoaiCSD == id_loaiCsd)
                         select p;
            return result.ToList();
        }

        public string InsertCSD(CSD csd)
        {
            try
            {
                csd.NgayTao = DateTime.Now;
                csd.NgayChinhSuaCuoi = DateTime.Now;
                csd.TrangThai = true;
                DB.AddToCSDs(csd);
                CommitChanges();
                return "true";
            }
            catch (System.Exception ex)
            {
                return ex.Message;
            }
            
        }
		public CSD LayCSD(int idcsd)
        {
            return DB.CSDs.FirstOrDefault(u => u.Id_CSD == idcsd);
        }

        public void CapNhatCSD(int id, CSD csd)
        {
            CSD temp = db.CSDs.Single(x => x.Id_CSD == id);
            temp.Ten = csd.Ten;
            temp.SoQDTL = csd.SoQDTL;
            temp.NgayThanhLap = csd.NgayThanhLap;
            temp.MoTa = csd.MoTa;

            db.SaveChanges();
        }

        public MANGHOATDONG LayMangHD()
        {
            return db.MANGHOATDONGs.First();
        }

        #endregion

    }
}